<?php

class main extends spController
{
    function __construct()
    { // 公用
        parent::__construct(); // 这是必须的

        $this->gdlink = spClass('link_list')->findAll();

        $this->banner5 = spClass('page_img')->findAll('nid=5');

        $this->pabtrows = spClass("article_list")->findAll("pid=49", "sort_id asc,id asc");

        $this->cabtrows = spClass("article_list")->findAll("pid=53", "sort_id asc,id asc");

        $this->rows = spClass("article_cat")->findAll("pid=4", "sort_id asc,id desc");


        $this->newrows = spClass("article_cat")->findAll("pid=4", "sort_id asc,id desc");

        $cat_select12 = spClass("goods_cat")->findAll("pid=0", "sort_id asc");
        foreach ($cat_select12 as $c_ss => $c_valuee) {
            $cat_select12[$c_ss]['second'] = spClass("goods_cat")->findAll("pid=" . $c_valuee['id'], "sort_id asc");
            foreach ($cat_select12[$c_ss]['second'] as $c_sss => $c_valueee) {
                $cat_select12[$c_ss]['second'][$c_sss]['third'] = spClass("goods_cat")->findAll("pid=" . $c_valueee['id'], "sort_id asc");
            }
        }
        $this->cat_select12 = $cat_select12;
    }

    function index()
    {
        $this->banner = spClass('page_img')->findAll('nid=1', 'sort_id asc,id desc');
        $this->sitesystem = spClass("site_system")->find();
        $this->zzrzlist = spClass('article_list')->findall('pid=91', 'sort_id asc,id asc');
        $this->yyanlicate = spClass("article_cat")->findAll("pid=78", "sort_id asc,id asc");
        $this->promotegoods = spClass('goods_list')->findAll('id>0 and promote=1', 'sort_id asc,id asc limit 6');
        $this->newslist = spClass('article_list')->findAll('pid=5', 'add_time desc limit 4');
        $this->cjwtlist = spClass('article_list')->findAll('pid=80', 'add_time desc limit 4');
        $this->gdlink = spClass('link_list')->findAll();
        $this->display("index/index.html");
    }

    function solution()
    {
        $result = spClass('article_list')->findSql("select * from my_article as A where exists(select 1 from my_article_cat AS B where A.pid=B.id AND B.pid=78)");
        $data = array();
        foreach ($result as $article) {
            if (!array_key_exists($article['pid'], $data)) {
                $data[$article['pid']] = array();
            }
            array_push($data[$article['pid']], $article);
        }
        echo $this->decodeUnicode(json_encode($data));
    }

    function decodeUnicode($str)
    {
        return preg_replace_callback('/\\\\u([0-9a-f]{4})/i',
            create_function(
                '$matches',
                'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'
            ),
            $str);
    }


    function getArticleCat($cat_id, $sort = 'add_time desc,sort_id asc', $num = 7)
    {
        $cat = spClass('article_cat')->find('id=' . $cat_id);
        $res = spClass('article_list')->findAll('pid=' . $cat_id . ' AND is_open=1', $sort, '', $num);
        foreach ($res as $rid => $rvalue) {
            $res[$rid]['cat'] = $cat['id'];
        }
        return $res;
    }

    function getArticleCatCount($cat_id, $sort = 'add_time desc,sort_id asc', $num = 7)
    {
        $res = spClass('article_list')->findCount('pid=' . $cat_id . ' AND is_open=1');
        if ($res >= 7) {
            $res = 7;
        }
        return $res;
    }

    function getshort()
    {
        $id = $_POST['id'];

        $row = spClass("article_list")->find("id=" . $id);

        $rows['title'] = "<a href='" . $row['link'] . "'>" . $row['title'] . "</a><br/><span>&nbsp;&nbsp;&nbsp;&nbsp;" . $row['short'] . "</span>";
        $rows = json_encode($rows);
        print_r($rows);
    }
}